package com.javaweb.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaweb.admin.entity.KdlGM;
import com.javaweb.admin.entity.KdlPurchasein;
import com.javaweb.admin.query.KdlGMQuery;
import com.javaweb.admin.query.KdlPurchaseinQuery;
import com.javaweb.admin.vo.kdlgm.KdlGMVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface KdlGMMapper extends BaseMapper<KdlGM> {
    @Select("select isnull(convert(varchar(20),max(sync_time),120),'2024-03-28 00:00:00') from kdl_gm")
    String GetMaxSyncTime();

    IPage<KdlGMVo> getGMList(IPage<KdlGMVo> page, @Param("req") KdlGMQuery req);

    IPage<KdlGMVo> getGMFeedbackList(IPage<KdlGMVo> page, @Param("req") KdlGMQuery req);


    @Select("select * from v_kdl_gm where orderno=#{orderno} order by itemno,batchno")
    List<KdlGMVo> getGMDetailList(String orderno);

    @Select("select distinct companycode,orderno,fromlocation,tolocation,plant from v_kdl_gm where orderno=#{orderno}")
    List<KdlGMVo> getGMCompanyList(String orderno);

    @Select("select distinct companycode,orderno,tolocation,plant from v_kdl_gm where orderno=#{orderno}")
    List<KdlGMVo> getOtherInCompanyList(String orderno);

    @Select("select distinct companycode,orderno,fromlocation,plant from v_kdl_gm where orderno=#{orderno}")
    List<KdlGMVo> getOtherOutCompanyList(String orderno);


    @Select("select * from v_kdl_gm where orderno=#{orderno} and companycode=#{companycode} and fromlocation=#{fromlocation} and tolocation=#{tolocation} and isnull(status,'未回写')<>'已回写' order by itemno,batchno")
    List<KdlGMVo> getCompanyGMDetailList(String orderno,String companycode,String fromlocation,String tolocation);

    @Select("select * from v_kdl_gm where orderno=#{orderno} and companycode=#{companycode} and tolocation=#{tolocation} and isnull(status,'未回写')<>'已回写' order by itemno,batchno")
    List<KdlGMVo> getCompanyGMOtherInDetailList(String orderno,String companycode,String tolocation);

    @Select("select * from v_kdl_gm where orderno=#{orderno} and companycode=#{companycode} and fromlocation=#{fromlocation} and isnull(status,'未回写')<>'已回写' order by itemno,batchno")
    List<KdlGMVo> getCompanyGMOtherOutDetailList(String orderno,String companycode,String fromlocation);

    @Select("select * from v_kdl_gm where principalorderno=#{principalorderno} order by itemno,batchno")
    List<KdlGMVo> getGMDetailListBySineOrder(String principalorderno);

    @Select("select distinct orderno,companycode from kdl_gm where isnull(status,'未回写')<>'已回写'")
    List<KdlGM> getGMSubmitList();

    @Select("select gm.* from kdl_gm gm left join kdl_qff_feedback feedback on gm.principalorderno=feedback.principalorderno  inner join kdl_purchasein po on feedback.orderno = po.vendorpo   where gm.orderno=#{orderno}")
    List<KdlGM> getPurchaseFeedback(String orderno);

    @Select("select gm.* from kdl_gm gm left join kdl_qff_feedback feedback on gm.principalorderno=feedback.principalorderno  inner join kdl_z05kdl so on feedback.orderno = so.vendorso   where gm.orderno=#{orderno}")
    List<KdlGM> getSaleFeedback(String orderno);

    @Select("select distinct feedback.orderno from kdl_gm gm left join kdl_qff_feedback feedback on gm.principalorderno=feedback.principalorderno where gm.orderno=#{orderno}")
    String getNCOrderno(String orderno);

    /**
     * 根据SAP批次号获取NC中
     * @param sapBatcno
     * @return
     */
    @Select("select * from ods_nc_scm_batchcode where vdef11=#{sapBatchno}")
    String getNCPKBatchcode(String sapBatcno);
}
